//
// Created by haoy on 2017/3/13.
//

#ifndef ANALYZER_RTP_INFO_HPP
#define ANALYZER_RTP_INFO_HPP

#include <algorithm>
#include <stdint.h>
#include <map>
#include <memory>

#include "dbg.hpp"
#include "rtp.h"
#include "video.h"
#include "vp8/vp8.h"
#include "media_hapi.h"
#include "packet.hpp"
#include "tlv.hpp"
#include "util.h"
#include "vpx/vpx_codec.h"
#include "vpx/vpx_decoder.h"
#include "vpx/vp8.h"
#include "vpx/vp8dx.h"


class Filters {
public:
    typedef std::shared_ptr<Filters> shared;
    enum Mask{
        NONE = 0,
        CODEC = 1 << 1,
        VIDEO = 1 << 2,
        AUDIO = 1 << 3,
        ALL = (0
            | CODEC
            | VIDEO
            | AUDIO
            )
    };

    enum LogLv {
        RTP = 1 << 1,
        RTCP = 1 << 2,
        SDP = 1 << 3,
        FRAME = 1 << 4,
        ALL_LOG = (0
            | SDP
            | RTP
            | RTCP
            | FRAME
            )
    };

    using MaskType = uint32_t;
    using LogLevel = uint32_t;

public:
    void setMask(const MaskType mask) {
        _mask = mask;
    }

    void addMask(const MaskType m) {
        _mask |= m;
    }

    MaskType getMask() {
        return _mask;
    }

    void removeMask(const MaskType m) {
        _mask &= (~m);
    }

    void setLogLevel(LogLevel lv) {
        _lv = lv;
    }

    void addLogLevel(LogLevel lv) {
        _lv |= lv;
    }

    void removeLogLevel(LogLevel lv) {
        _lv &= (~lv);
    }

    LogLevel getLogLevel() {
        return _lv;
    }

    void setRollNumber(int roll) {
        _roll = roll;
    }

    int getRollNumber() {
        return _roll;
    }

private:
    MaskType _mask = ALL;
    LogLevel _lv = ALL_LOG;
    int _roll = 0;
};

class RTPAnalyzer : public PacketHandler {
public:
    typedef std::shared_ptr<RTPAnalyzer> shared;
public:

    // mediacodec_id_t getAudioCodec();
    // mediacodec_id_t getVideoCodec();
    // uint32_t getVideoWidth();
    // uint32_t getVideoHeight();
    // uint32_t getAudioSampleRate();
    // uint32_t getAudioChannels();
    virtual void analyzer(const std::string& filepath, const Filters::shared filter) = 0;

    static shared Create();
private:
    virtual void onCodecRegistry(CodecRegistry& codecs, uint8_t pt) = 0;

    virtual void onAudioPacket(RTPPacket* packet, uint64_t pts) = 0;

    virtual void onVideoPacket(RTPPacket* packet, uint64_t pts) = 0;

    virtual void onEnd() = 0;
};

#endif //ANALYZER_RTP_INFO_HPP
